package com.phelon.epdm;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.BooleanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.phelon.systemLink.SessionInfo;
import com.phelon.systemLink.SessionManager;

public class BillOfMaterialXMLBuilder extends XMLBuilder {
	

	private BillOfMaterial 	theBom;

	
	public static void main(String[] args) {
		BillOfMaterial testBom = new BillOfMaterial("A  ", "ASP9802536  ", "      ");
		BillOfMaterialXMLBuilder builder = new BillOfMaterialXMLBuilder(testBom, "7M");
		builder.doCreate();
		System.exit(0);
			}
	public BillOfMaterialXMLBuilder(BillOfMaterial theBom, String toEnv) {
		this.theBom = theBom;
		setEnvironmentID(toEnv);
	}
	
	public void buildCreateXML(SessionInfo theSession) {
		setDocument(DocumentHelper.createDocument());
		
		getDocument().addDocType("System-Link", "SYSTEM", "SystemLinkRequest.dtd");
		
		Element root = getDocument().addElement("System-Link").addAttribute("formatForDisplay", "true");
		
		Element request = root.addElement("Request").addAttribute("sessionHandle"
				,theSession == null ? "*current" : theSession.getSessionHandle()).addAttribute(
				"workHandle", "*new").addAttribute("broker","EJB").addAttribute("maxIdle", "1000");
		Element domainEntity = request.addElement("Create").addAttribute("name",
				"queryObject_BillOfMaterial").addAttribute("domainClass","com.mapics.epdm.BillOfMaterial")
				.addAttribute("retainResult", "true").addElement("DomainEntity");
		Element key = domainEntity.addElement("Key");
		key.addElement("Property").addAttribute("path", "site").addElement("Value")
				.addCDATA(theBom.getSite());
        key.addElement("Property").addAttribute("path", "parentItem").addElement("Value")
        		.addCDATA(theBom.getParentItem());
        key.addElement("Property").addAttribute("path", "parentItemRevision").addElement("Value")
        		.addCDATA(theBom.getParentItemRevision());;  
/*        domainEntity.addElement("Property").addAttribute("path", "apcItem").addElement("Value").addCDATA(
        		theItem.getApcItem()); */
        domainEntity.addElement("Property").addAttribute("path", "alternateBomId").addElement("Value").addCDATA(
                theBom.getAlternateBomId());
        domainEntity.addElement("Property").addAttribute("path", "description").addElement("Value").addCDATA(
                theBom.getDescription());
        domainEntity.addElement("Property").addAttribute("path", "standardBatchQuantity").addElement("Value").addCDATA(
                theBom.getStandardBatchQuantity());
        domainEntity.addElement("Property").addAttribute("path", "userFieldDate1").addElement("Value").addCDATA(
                theBom.getUserFieldDate1());
/*        domainEntity.addElement("Property").addAttribute("path", "userFieldSwitchA").addElement("Value").addCDATA(
                theBom.getUserFieldSwitchA()); */	
        domainEntity.addElement("Property").addAttribute("path", "userFieldText40").addElement("Value").addCDATA(
                theBom.getUserFieldText40());
        for (BillOfMaterialComponent c : theBom.getComponents()){
    		domainEntity = request.addElement("Create").addAttribute("name",
                    "line" + c.getUserSequence1() + c.getComponentItem() + c.getUserSequence2())
                    .addAttribute("domainClass","com.mapics.epdm.BillOfMaterialComponent")
                    .addAttribute("retainResult", "true").addElement("DomainEntity");
			key = domainEntity.addElement("Key");
			key.addElement("Property").addAttribute("path", "site").addElement("Value")
					.addCDATA(c.getSite());
		    key.addElement("Property").addAttribute("path", "parentItem").addElement("Value")
		    		.addCDATA(c.getParentItem());
		    key.addElement("Property").addAttribute("path", "parentItemRevision").addElement("Value")
		    		.addCDATA(c.getParentItemRevision()); 
			key.addElement("Property").addAttribute("path", "alternateBomId").addElement("Value")
					.addCDATA(c.getAlternativeBomId());
/*	        key.addElement("Property").addAttribute("path", "userSequence1").addElement("Value").addCDATA(
	                c.getUserSequence1()); */	        
		    key.addElement("Property").addAttribute("path", "componentItem").addElement("Value")
		    		.addCDATA(c.getComponentItem());
		    key.addElement("Property").addAttribute("path", "componentItemRevision").addElement("Value")
		    		.addCDATA(c.getComponentItemRevision());         	
/*	        key.addElement("Property").addAttribute("path", "userSequence2").addElement("Value").addCDATA(
	                c.getUserSequence2());  */	        
	        domainEntity.addElement("Property").addAttribute("path", "componentLeadTimeAdjustment").addElement("Value").addCDATA(
	                c.getComponentLeadTimeAdjustment());	
	        domainEntity.addElement("Property").addAttribute("path", "effectiveFromDate").addElement("Value").addCDATA(
	                c.getEffectiveFromDate());
	        domainEntity.addElement("Property").addAttribute("path", "effectiveToDate").addElement("Value").addCDATA(
	                c.getEffectiveToDate());
	        domainEntity.addElement("Property").addAttribute("path", "featureOptionCode").addElement("Value").addCDATA(
	                c.getFeatureOptionCode());	
	        domainEntity.addElement("Property").addAttribute("path", "featureOptionCostRollUpFactor").addElement("Value").addCDATA(
	                c.getFeatureOptionCostRollUpFactor());
	        domainEntity.addElement("Property").addAttribute("path", "featureOptionNumber").addElement("Value").addCDATA(
	                c.getFeatureOptionNumber());
	        domainEntity.addElement("Property").addAttribute("path", "featureOptionPlanningFactor").addElement("Value").addCDATA(
	                c.getFeatureOptionPlanningFactor());	
	        domainEntity.addElement("Property").addAttribute("path", "operationWhereFirstUsed").addElement("Value").addCDATA(
	                c.getOperationWhereFirstUsed());
	        domainEntity.addElement("Property").addAttribute("path", "quantityPerUnit").addElement("Value").addCDATA(
	                c.getQuantityPerUnit());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldCodeA").addElement("Value").addCDATA(
	                c.getUserFieldCodeA());	
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity1").addElement("Value").addCDATA(
	                c.getUserFieldQuantity1());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity2").addElement("Value").addCDATA(
	                c.getUserFieldQuantity2());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity3").addElement("Value").addCDATA(
	                c.getUserFieldQuantity3());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity4").addElement("Value").addCDATA(
	                c.getUserFieldQuantity4());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity5").addElement("Value").addCDATA(
	                c.getUserFieldQuantity5());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity6").addElement("Value").addCDATA(
	                c.getUserFieldQuantity6());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity7").addElement("Value").addCDATA(
	                c.getUserFieldQuantity7());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldShortAlphaA").addElement("Value").addCDATA(
	                c.getUserFieldShortAlphaA());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldShortQuantity1").addElement("Value").addCDATA(
	                c.getUserFieldShortQuantity1());	
	        domainEntity.addElement("Property").addAttribute("path", "userFieldDate1").addElement("Value").addCDATA(
	                c.getUserFieldDate1());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldText40").addElement("Value").addCDATA(
	                c.getUserFieldText40());
        }

        
 	}  
}

	